package backs.app;

import backs.bean.Message;

import java.util.ArrayList;
import java.util.List;

public class AppMenu extends App
{
    public String name;
    public String show;
    public AppMenu fatherMenu;
    public String fathersName="";
    public List<AppMenu> menus= new ArrayList<AppMenu>();
    public boolean showSon=true;
    public AppMenu(String appName,int index,String name,String show,AppMenu fatherMenu)
    {
        super(appName,index);
        this.name=name;
        this.show=show;
        this.fatherMenu=fatherMenu;
        if(fatherMenu!=null)this.fathersName=fatherMenu.fathersName+fatherMenu.name;
    }
    public AppMenu(String name,String show,AppMenu fatherMenu)
    {
        super("menu");
        this.name=name;
        this.show=show;
        this.fatherMenu=fatherMenu;
        if(fatherMenu!=null)this.fathersName=fatherMenu.fathersName+fatherMenu.name;
    }

    public String getNameAndShow()
    {
        return fathersName+name+": \t"+show;
    }

    public void addSonMenu(AppMenu menu)
    {
        menus.add(menu);
    }

    public String getSonMenusShow()
    {
        String show="---"+name+"---\n";
        for(AppMenu menu:menus)
        {
            show+=menu.getNameAndShow()+"\n";
        }
        show.substring(0,show.length()-1);
        return show;
    }

    @Override
    public void handleMessage(Message message)
    {
        if(message.message.equals(name))
        {
            if(showSon)
            {
                message.sendBack(getSonMenusShow());
            }
            handleEqual(message);
            message.notHandled=false;
        }
        else if(message.message.startsWith(name))
        {
            message.notHandled=false;
            message.message=message.message.substring(name.length());
            for(AppMenu menu:menus)
            {
                menu.handleMessage(message);
            }
            handleStart(message);
        }
    }

    public void handleEqual(Message message)
    {

    }

    public void handleStart(Message message)
    {

    }
}
